<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="主键ID" prop="id">
                        <el-input v-model.number="dataForm.id" placeholder="主键ID"></el-input>
                </el-form-item>
                <el-form-item label="风险类型（如信用风险、市场风险、操作风险）" prop="riskType">
                        <el-input v-model="dataForm.riskType" placeholder="风险类型（如信用风险、市场风险、操作风险）"></el-input>
                </el-form-item>
                <el-form-item label="业务场景" prop="businessScenario">
                        <el-input v-model="dataForm.businessScenario" placeholder="业务场景"></el-input>
                </el-form-item>
                <el-form-item label="违约概率（PD）" prop="pd">
                        <el-input v-model="dataForm.pd" placeholder="违约概率（PD）"></el-input>
                </el-form-item>
                <el-form-item label="违约损失率（LGD）" prop="lgd">
                        <el-input v-model="dataForm.lgd" placeholder="违约损失率（LGD）"></el-input>
                </el-form-item>
                <el-form-item label="风险暴露（EAD）" prop="ead">
                        <el-input v-model="dataForm.ead" placeholder="风险暴露（EAD）"></el-input>
                </el-form-item>
                <el-form-item label="市场风险的VaR值" prop="var">
                        <el-input v-model="dataForm.var" placeholder="市场风险的VaR值"></el-input>
                </el-form-item>
                <el-form-item label="压力测试结果" prop="stressTestResult">
                        <el-input v-model="dataForm.stressTestResult" placeholder="压力测试结果"></el-input>
                </el-form-item>
                <el-form-item label="操作风险的损失分布" prop="lossDistribution">
                        <el-input v-model="dataForm.lossDistribution" placeholder="操作风险的损失分布"></el-input>
                </el-form-item>
                <el-form-item label="评估日期" prop="assessmentDate">
                        <el-date-picker
                                v-model="dataForm.assessmentDate"
                                type="datetime"
                                placeholder="评估日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="模型校准日期" prop="modelCalibrationDate">
                        <el-date-picker
                                v-model="dataForm.modelCalibrationDate"
                                type="datetime"
                                placeholder="模型校准日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="模型准确性" prop="modelAccuracy">
                        <el-input v-model="dataForm.modelAccuracy" placeholder="模型准确性"></el-input>
                </el-form-item>
                <el-form-item label="模型有效性" prop="modelEffectiveness">
                        <el-input v-model="dataForm.modelEffectiveness" placeholder="模型有效性"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            id: null,
            riskType: null,
            businessScenario: null,
            pd: null,
            lgd: null,
            ead: null,
            var: null,
            stressTestResult: null,
            lossDistribution: null,
            assessmentDate: null,
            modelCalibrationDate: null,
            modelAccuracy: null,
            modelEffectiveness: null,
            createdAt: null,
            updatedAt: null
          },
          dataRule: {
            id: [
                        { required: true, message: '主键ID不能为空', trigger: 'blur' }
            ],
            riskType: [
                        { required: true, message: '风险类型（如信用风险、市场风险、操作风险）不能为空', trigger: 'blur' }
            ],
            businessScenario: [
                        { required: true, message: '业务场景不能为空', trigger: 'blur' }
            ],
            pd: [
                        { required: true, message: '违约概率（PD）不能为空', trigger: 'blur' }
            ],
            lgd: [
                        { required: true, message: '违约损失率（LGD）不能为空', trigger: 'blur' }
            ],
            ead: [
                        { required: true, message: '风险暴露（EAD）不能为空', trigger: 'blur' }
            ],
            var: [
                        { required: true, message: '市场风险的VaR值不能为空', trigger: 'blur' }
            ],
            stressTestResult: [
                        { required: true, message: '压力测试结果不能为空', trigger: 'blur' }
            ],
            lossDistribution: [
                        { required: true, message: '操作风险的损失分布不能为空', trigger: 'blur' }
            ],
            assessmentDate: [
                        { required: true, message: '评估日期不能为空', trigger: 'blur' }
            ],
            modelCalibrationDate: [
                        { required: true, message: '模型校准日期不能为空', trigger: 'blur' }
            ],
            modelAccuracy: [
                        { required: true, message: '模型准确性不能为空', trigger: 'blur' }
            ],
            modelEffectiveness: [
                        { required: true, message: '模型有效性不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.id = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.id = id || 0
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(`/app/risk_quantitative/info/${this.dataForm.id}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appriskquantitative
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/risk_quantitative/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>